import 'package:flutter/material.dart';
import 'package:flutter_scankit/flutter_scankit.dart';

class HuaweiScan extends StatefulWidget {
  const HuaweiScan({super.key});

  @override
  State<HuaweiScan> createState() => _CustomViewState();
}

class _CustomViewState extends State<HuaweiScan> with TickerProviderStateMixin {
  final ScanKitController _controller = ScanKitController();

  @override
  void initState() {
    _controller.onResult.listen((result) {
      Future(() {
        Navigator.of(context).pop(result.originalValue);
      });
    });
    super.initState();
  }

  @override
  void dispose() {
    _controller.dispose();
    super.dispose();
  }

  Widget _iconButton({content, callBack}) {
    return Container(
      width: 30,
      height: 30,
      decoration: const BoxDecoration(
        shape: BoxShape.circle,
        color: Colors.white,
      ),
      child: IconButton(icon: content, onPressed: callBack),
    );
  }

  @override
  Widget build(BuildContext context) {
    var screenWidth = MediaQuery.of(context).size.width;
    var screenHeight = MediaQuery.of(context).size.height;
    var rect = Rect.fromLTWH(0, 0, screenWidth, screenHeight);

    return Scaffold(
      body: Stack(
        children: [
          ScanKitWidget(
              controller: _controller,
              continuouslyScan: false,
              boundingBox: rect),
          Positioned(
            top: 40,  // 调整顶部间距
            left: 20, // 调整左侧间距
            child: _iconButton(
              content: const Icon(Icons.arrow_back, color: Colors.black, size: 14), // 可调整图标大小
              callBack: () {
                Navigator.of(context).pop();
              },
            ),
          ),
          Align(
              alignment: Alignment.bottomCenter,
              child: Padding(
                padding: const EdgeInsets.fromLTRB(20, 0, 20, 0),
                child: Column(
                  mainAxisSize: MainAxisSize.min,
                  children: [
                    Row(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      children: [
                        _iconButton(
                            content: const Icon(Icons.lightbulb_outline_rounded,
                                color: Colors.black, size: 14),
                            callBack: () {
                              _controller.switchLight();
                            }),
                        _iconButton(
                            content: const Icon(Icons.panorama,
                                color: Colors.black, size: 14),
                            callBack: () {
                              _controller.pickPhoto();
                            }),
                      ],
                    ),
                    const SizedBox(height: 50)
                  ],
                ),
              )),
          const AnimatedLinePage()
        ],
      ),
    );
  }
}

class AnimatedLinePage extends StatefulWidget {
  const AnimatedLinePage({super.key});

  @override
  State<AnimatedLinePage> createState() => _AnimatedLinePageState();
}

class _AnimatedLinePageState extends State<AnimatedLinePage>
    with SingleTickerProviderStateMixin {
  late AnimationController _controller;
  late Animation<double> _animation;
  double lineHeight = 100.0;
  late double screenHeight;

  @override
  void initState() {
    super.initState();

    WidgetsBinding.instance.addPostFrameCallback((_) {
      screenHeight = MediaQuery.of(context).size.height;

      _controller = AnimationController(
        vsync: this,
        duration: const Duration(seconds: 5),
      );

      // 初始化Tween和动画
      _updateAnimation(100, screenHeight - 200);
      _controller.forward();
    });
  }

  void _updateAnimation(double begin, double end) {
    _animation = Tween<double>(begin: begin, end: end).animate(
      CurvedAnimation(
        parent: _controller,
        curve: Curves.linear,
      ),
    );

    _controller.addListener(() {
      setState(() {
        lineHeight = _animation.value;
        if (lineHeight >= screenHeight - 200) {
          _controller.stop();
          _controller.reset();
          _updateAnimation(100, screenHeight - 200);
          _controller.forward();
        }
      });
    });
  }

  @override
  void dispose() {
    _controller.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Positioned(
      top: lineHeight,
      left: 0,
      right: 0,
      child: Container(
        height: 2,
        margin: const EdgeInsets.all(30),
        decoration: const BoxDecoration(
          shape: BoxShape.rectangle,
          gradient: LinearGradient(
            begin: Alignment.centerLeft,
            end: Alignment.centerRight,
            colors: [
              Colors.transparent,
              Colors.green,
              Colors.green,
              Colors.transparent,
            ],
            stops: [0.0, 0.4, 0.6, 1.0],
          ),
        ),
      ),
    );
  }
}
